var pageNo;     //当前页码
var pageCount;  //总页数
var pageCount02;  //总页数
$(function () {
    /*info(pageNo);*/
    pageloddd($("#totalElements").val(),$("#pageNo").val(),5);

    /*$("#select").click(function () {
        info(pageNo);
    });*/

    $("#commodityName").val($("#zhi01").text());
    $("#startDate").val($("#zhi02").text());
    $("#endDate").val($("#zhi03").text());
    var imgzhi=0;
    imgzhi=$("#zhi04").text();
    $("#imgId").val(imgzhi);
});



//分页
function pageloddd(totalElements,pageNo,pageNum){
    layui.use(['laypage'], function(){
        var laypage = layui.laypage;
        //执行一个laypage实例
        var a=0;
        a=a+parseInt(pageNo)+1;
        laypage.render({
            elem: 'this', //注意，这里的 test1 是 ID，不用加 # 号
            count: totalElements //数据总数，从服务端得到
            ,layout: ['count', 'prev', 'page', 'next', 'skip']
            ,curr:a
            ,limit:pageNum  //每一页显示的条数
            ,skip:true
            ,jump: function(obj, first) {
                //obj包含了当前分页的所有参数，比如：
                console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
                console.log(obj.limit); //得到每页显示的条数
                var aaa=(obj.curr-1);
                if(!first){
                    var imgzhi02=0;
                    imgzhi02=$("#zhi04").text();
                    location.href="/Comment/getCommentList?pageNo="+aaa+"&commodityName="+$("#zhi01").text()+"&startDate="+$("#zhi02").text()+"&endDate="+$("#zhi03").text()+"&imgId="+imgzhi02;
                }
            }
        });
    });
}





/*查看子评论*/
function zipinglun_view(id) {

    var index = layer.open({
        type: 1,
        title: '子评论',
        maxmin: true,
        shadeClose: false,
        area: ['600px', '350px'],
        offset: ['100px', '280px'],
        content: $('#Guestbook')
    });



    $("#zipinglun").empty();
    $.getJSON("/Comment/getZiCommentList", "belongtoid=" + id+"&size="+100,function (result) {

        if(result.length>0){
            $.each(result, function (i, e) {
                var zhuangtai;

                if (e.status == 1) {
                    zhuangtai = "<span class='label label-success radius'>已显示</span>";
                } else {
                    zhuangtai = "<span class='label label-default radius'>已隐藏</span>";
                }
                $("#zipinglun").append(
                    "<tr>"
                    + "<td>" + e.commentid + "</td>"
                    + "<td>" + e.userName + "</td>"
                    + "<td>" + e.content + "</td>"
                    + "<td  class='td-status'>" + zhuangtai + "</td>"
                    + "</tr>");
            });
        }else{
            $("#zipinglun").append("<p style='font-size: 20px;color: #CCCCCC; align:center;'>没有子评论</p>");
        }

    });

    /*show(pageNo02,id);*/
};

/*显示子评论*/
function show(pageNo02, id) {
    alert("id" + id);
    $("#zipinglun").empty();
    $.getJSON("/Comment/getZiCommentList", "belongtoid=" + id + "&pageNo=" + pageNo02, function (result) {
        pageCount02 = result.totalPages;
        $.each(result.content, function (i, e) {
            var zhuangtai;
            var statusbtn;  //状态按钮,1可见，2不可见

            if (e.status == 1) {
                zhuangtai = "<span class='label label-success radius'>已显示</span>";
                statusbtn = "<a onClick='member_stop(this," + e.commentid + ")'  href='javascript:;' title='隐藏'  class='btn btn-xs btn-status'>隐藏</a>";

            } else {
                zhuangtai = "<span class='label label-default radius'>已隐藏</span>";
                statusbtn = "<a onClick='member_start(this," + e.commentid + ")'  href='javascript:;' title='显示'  class='btn btn-xs'>显示</a>";
            }
            $("#zipinglun").append(
                "<tr>"
                + "<td>" + e.commentid + "</td>"
                + "<td>" + e.userid + "</td>"
                + "<td>" + e.content + "</td>"
                + "<td  class='td-status'>" + zhuangtai + "</td>"
                + "<td  class='td-manage'>" + statusbtn + "</td>"
                + "</tr>");
        });
        page02(pageNo02, id);
    });
}



/*查看图片*/
function view_image(id) {
    var index = layer.open({
        type: 1,
        title: '图片',
        maxmin: true,
        shadeClose: false,
        area: ['600px', ''],
        content: $('#images')
    });
    show_images(id);
};

/*显示图片*/
function show_images(id) {
    $.getJSON("/Comment/getCommentImages", "id=" + id, function (result) {
        $("#images").empty();
        $.each(result, function (i, e) {
            $("#images").append("<img src='" + e + "' style='width: 150px;height: 100px;'/>");
        })
    });
}




/*产品-停用*/
function member_stop(obj, id) {
    layer.confirm('确认要隐藏该评论吗？', function (index) {
        $.getJSON("/Comment/updateByStatus", "commentid=" + id + "&status=" + 2, function (result) {
            if (result == true) {
                $(obj).parents("tr").find(".td-manage").prepend('<a style="text-decoration:none" class="btn btn-xs " onClick="member_start(this,' + id + ')" href="javascript:;" title="显示">显示</a>');
                $(obj).parents("tr").find(".td-status").html('<span class="label label-default radius">已隐藏</span>');
                $(obj).remove();
                layer.msg('已隐藏!', {icon: 5, time: 1000});
            } else {
                layer.msg('隐藏失败!', {icon: 5, time: 1000});
            }
        });
    });
}

/*产品-启用*/
function member_start(obj, id) {
    layer.confirm('确认要显示该评论吗？', function (index) {
        $.getJSON("/Comment/updateByStatus", "commentid=" + id + "&status=" + 1, function (result) {
            if (result == true) {
                $(obj).parents("tr").find(".td-manage").prepend('<a style="text-decoration:none" class="btn btn-xs btn-status" onClick="member_stop(this,' + id + ')" href="javascript:;" title="显示">显示</a>');
                $(obj).parents("tr").find(".td-status").html('<span class="label label-success radius">已隐藏</span>');
                $(obj).remove();
                layer.msg('已显示!', {icon: 6, time: 1000});
            } else {
                layer.msg('显示失败!', {icon: 5, time: 1000});
            }
        });

    });
}


//主评论分页
function page(pageNo) {
    var $page = $("#page").empty();   //清除分页框

    if (pageNo > 1) {
        //显示首页,上一页
        var $firstPage = $("<input type='button' class='btn btn-xs btn-info' style='padding:3% 5%' name='btn' value='首页' id='firstPage'/>");
        var $prePage = $("<input type='button' class='btn btn-xs btn-info' style='padding:3% 5%' name='btn' value='上一页' id='prePage'/>");
        $page.append($firstPage).append($prePage);
    }
    if (pageNo < pageCount) {
        //显示尾页,下一页
        var $nextPage = $("<input type='button' class='btn btn-xs btn-info' style='padding:3% 5%' name='btn' value='下一页' id='nextPage'/>");
        var $lastPage = $("<input type='button' class='btn btn-xs btn-info' style='padding:3% 5%' name='btn' value='尾页' id='lastPage'/>");
        $page.append($nextPage).append($lastPage);
    }
    $page.append("[当前页码" + pageNo + "/" + pageCount + "总页数]");
    $("#firstPage").click(function () {
        pageNo = 1;
        info(pageNo);
    });
    $("#prePage").click(function () {
        pageNo -= 1;
        info(pageNo);
    });
    $("#nextPage").click(function () {
        pageNo += 1;
        info(pageNo);
    });
    $("#lastPage").click(function () {
        pageNo = pageCount;
        info(pageNo);
    });
}

//子评论分页
function page02(pageNo02, id) {
    var $page = $("#page02").empty();   //清除分页框

    if (pageNo02 > 1) {
        //显示首页,上一页
        var $firstPage = $("<input type='button' class='btn btn-xs btn-info' style='padding:3% 5%' name='btn' value='首页' id='firstPage02'/>");
        var $prePage = $("<input type='button' class='btn btn-xs btn-info' style='padding:3% 5%' name='btn' value='上一页' id='prePage02'/>");
        $page.append($firstPage).append($prePage);
    }
    if (pageNo02 < pageCount02) {
        //显示尾页,下一页
        var $nextPage = $("<input type='button' class='btn btn-xs btn-info' style='padding:3% 5%' name='btn' value='下一页' id='nextPage02'/>");
        var $lastPage = $("<input type='button' class='btn btn-xs btn-info' style='padding:3% 5%' name='btn' value='尾页' id='lastPage02'/>");
        $page.append($nextPage).append($lastPage);
    }
    $page.append("[当前页码" + pageNo02 + "/" + pageCount02 + "总页数]");
    $("#firstPage02").click(function () {
        pageNo02 = 1;
        show(pageNo02, id);
    });
    $("#prePage02").click(function () {
        pageNo02 -= 1;
        show(pageNo02, id);
    });
    $("#nextPage02").click(function () {
        pageNo02 += 1;
        show(pageNo02, id);
    });
    $("#lastPage02").click(function () {
        pageNo02 = pageCount02;
        show(pageNo02, id);
    });
}



/*显示主评论*/
/*
function info(pageNo) {
    /!*$("#showTable").empty();    //清除显示内容*!/

    //获取条件
    var commodityId = $("#commodityid").val();     //商品id
    var startDate = $("#test1").val();    //开始时间
    var endDate = $("#test2").val();    //结束时间
    var imgId = $("#imgid option:selected").val();    //是否有图

    var params = "commodityId=" + commodityId + "&imgId=" + imgId + "&startDate=" + startDate + "&endDate=" + endDate + "&belongtoid=" + 0 + "&pageNo=" + pageNo;

    $.getJSON("/Comment/getCommentList", params, function (result) {

        $("#zongshu").text(result.totalElements);   //设置总数
        pageCount = result.totalPages;

        $.each(result.content, function (i, e) {
            var zhuangtai;
            var statusbtn;  //状态按钮,1可见，2不可见
            var tupian = "&nbsp;<a href='javascript:;' title='图片信息' class='btn btn-xs  bg-deep-blue' onclick='view_image(" + e.commentid + ")\'>图片详情</a>";
            if (e.status == 1) {
                zhuangtai = "<span class='label label-success radius'>已显示</span>";
                statusbtn = "<a onClick='member_stop(this," + e.commentid + ")'  href='javascript:;' title='隐藏'  class='btn btn-xs btn-status'>隐藏</a>";
            } else {
                zhuangtai = "<span class='label label-default radius'>已隐藏</span>";
                statusbtn = "<a onClick='member_start(this," + e.commentid + ")'  href='javascript:;' title='显示'  class='btn btn-xs'>显示</a>";
            }
            $("#showTable").append(
                "<tr>"
                + "<td>" + e.commentid + "</td>"
                + "<td>" + e.userid + "</td>"
                + "<td>" + e.commodityid + "</td>"
                + "<td><a href='javascript:;' onclick='zipinglun_view(" + e.commentid + ")'>" + e.content + "</a></td>"
                + "<td>" + e.commenttime + "</td>"
                + "<td>" + e.likecount + "</td>"
                + "<td  class='td-status'>" + zhuangtai + "</td>"
                + "<td  class='td-manage'>" + statusbtn + tupian + "</td>"
                + "</tr>");
        });
        page(pageNo);
    });
}
*/

